import { _decorator, Component, Node, Vec3, Label, UITransform, Color, Sprite, Input, EventTouch } from 'cc';
import CCUtils from '../util/CCUtils';
import { LayerInfo } from './GameConst';
import GImage from './GImage';
import { LayerItem } from './LayerItem';
import { PosInfo } from './PosInfo';
const { ccclass, property } = _decorator;

@ccclass('CardItem')
export class CardItem extends Component {

    /**他属于的layer，用来方便查找位置 */
    public belongLayer:LayerItem = null;
    public type:number = 0;
    public pos:PosInfo = null;

    private bg:GImage = null;
    private showImg:GImage = null;
    private posLabel:Label = null;
    private selectImg:Node = null;
    private removeBtn:Node = null;
    private redMask:Node = null;
    

    private _beCover:boolean = false;
    private _select:boolean = false;

    public static nodeSiblingIndex:number = -1;

    onLoad(){
        this.bg = CCUtils.getGImage(this.node,"bg");
        this.showImg = CCUtils.getGImage(this.node,"showImg");
        this.posLabel = CCUtils.getLabel(this.node,"posLabel");
        this.selectImg = CCUtils.findChild(this.node,"selectImg");
        this.removeBtn = CCUtils.findChild(this.node,"removeBtn");
        this.removeBtn.on(Input.EventType.TOUCH_END, this.onRemoveCard, this);
        this.redMask = CCUtils.findChild(this.node,"redMask");
        // this.bg.node.on(Input.EventType.TOUCH_START, this.onClickNode, this);
    }


    /**
     * 初始化卡片
     * @param type 
     * @param pos 
     */
    public init(belongLayer:LayerItem,type:number, pos:PosInfo = null) {
        if (belongLayer) {
            this.belongLayer = belongLayer;
            // this.bg.loadSpriteFrame(`ui/bg${this.belongLayer.info.layerId%10}/spriteFrame`);
        }
        this.bg.loadSpriteFrame(`ui/cardBg/spriteFrame`);
        
        this.type = type;
        
        this.showImg.loadSpriteFrame(`ui/card${this.type}/spriteFrame`);
        
        // console.log("初始化位置", this.pos.pixelX, this.pos.pixelY);
        // this.posLabel.string = `${this.pos.gridX}_${this.pos.gridY}`;
        // this.posLabel.string = `${this.belongLayerId}_${CardItem.nodeSiblingIndex}`;
        // this.posLabel.string = `${this.belongLayerId}`;
        // this.node.setSiblingIndex(CardItem.nodeSiblingIndex);
        // CardItem.nodeSiblingIndex --;    
    }

    /**
     * setPos
     */
    public setPos(pos:PosInfo) {
        this.pos = pos;
        this.node.setPosition(this.pos.pixelX, this.pos.pixelY);
    }

    public get beCover() : boolean {
        return this._beCover;
    }

     /**
     * 是否被覆盖，true为被覆盖了，应该不能点，灰色
     */
    public set beCover(v : boolean) {
        this._beCover = v;
        // console.log("是否被覆盖",this.belongLayerId, this._beCover, this.pos.gridX, this.pos.gridY);
        // this.bg.color = this._beCover ? new Color(150, 137, 137, 255):new Color(255, 255, 255, 255);

        // this.mask.active = this._beCover;
        this.showImg.grayscale = this._beCover;
        this.bg.grayscale = this._beCover;
    }


    public set select(v : boolean) {
        this._select = v;
        this.selectImg.active = this._select;
    }
    
    public get select() : boolean {
        return this._select
    }


    // private onClickNode(e:EventTouch) {
    //     if (this.pos == null) {
    //         this.removeBtn.active = false;
    //     }else{
    //         this.removeBtn.active = !this.removeBtn.active;
    //     }
    // }

    /**移除卡牌 */
    private onRemoveCard(e:EventTouch) {
        this.belongLayer.removeCard(this);
    }


    public changeRedMask(boo:boolean){
        this.redMask.active = boo;
    }
    
    
}


